package com.isunland.managesystem.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.SimpleNetworkCallBackDialogParams;
import com.isunland.managesystem.utils.LocalSearch;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import org.json.JSONException;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/6/23 16:12
 */

public class SimpleNetworkMultiCallBackDialogFragment<T> extends BaseDialogFragment {

  private SimpleNetworkCallBackDialogParams mParams;
  private ArrayList<T> mList;
  private ArrayAdapter<T> mAdapter;
  private AlertDialog mProgressDialog;
  private AlertDialog mDialog;
  private CallBack<T> mCallBack;
  private AbsListView mListView;

  @Override public void initData() {
    super.initData();
    mParams = !(mBaseParams instanceof SimpleNetworkCallBackDialogParams)
        ? new SimpleNetworkCallBackDialogParams() : (SimpleNetworkCallBackDialogParams) mBaseParams;
    mList = new ArrayList<>();
    mAdapter =
        new ArrayAdapter<T>(getActivity(), android.R.layout.select_dialog_singlechoice, mList) {

          @NonNull @Override
          public View getView(int position, View convertView, @NonNull ViewGroup parent) {
            if (convertView == null) {
              convertView =
                  LayoutInflater.from(getContext()).inflate(getItemResId(), parent, false);
            }
            TextView tvName = (TextView) convertView.findViewById(android.R.id.text1);
            tvName.setText(LocalSearch.getFieldValue(getItem(position), mParams.getShowField()));
            return convertView;
          }
        };
  }

  protected int getItemResId() {
    return android.R.layout.select_dialog_multichoice;
  }

  @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);
    AlertDialog.Builder builderSingle = new AlertDialog.Builder(getActivity());
    View v = mActivity.getLayoutInflater().inflate(R.layout.dialog_progress, null);
    builderSingle.setView(v);
    mProgressDialog = builderSingle.create();
    mDialog = new AlertDialog.Builder(mActivity).setTitle(R.string.pleaseSelect)
        .setAdapter(mAdapter, null)
        .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {
            //默认操作,隐藏dialog.
          }
        })
        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {
            //T base = mAdapter.getItem(which);
            //if (mCallBack != null) mCallBack.select(base);
            if (mAdapter.getCount() < 1) return;
            ArrayList<T> list = new ArrayList<>();
            for (int i = mAdapter.getCount() - 1; i >= 0; i--) {
              if (mListView.isItemChecked(i)) {
                list.add(mList.get(i));
              }
            }
            if (mCallBack != null) mCallBack.select(list);
          }
        })
        .create();
    mListView = mDialog.getListView();
    mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    volleyPost();
    return mProgressDialog;
  }

  public SimpleNetworkMultiCallBackDialogFragment setCallBack(CallBack<T> callBack) {
    mCallBack = callBack;
    return this;
  }

  public interface CallBack<T> {
    void select(ArrayList<T> base);
  }

  private void volleyPost() {
    mActivity.volleyPost(mParams.getUrl(), mParams.getparameters(), new VolleyResponse() {

      @Override public void onVolleyResponse(String json) throws JSONException {

        mProgressDialog.dismiss();
        //BaseOriginal<T> result = inflate(json);
        @SuppressWarnings("unchecked") ArrayList<T> rows =
            new Gson().fromJson(json, mParams.getClassOriginal()).getRows();
        if (rows.size() == 0) {
          ToastUtil.show(R.string.empty_data);
          return;
        }
        mList.clear();
        mList.addAll(rows);
        mAdapter.notifyDataSetChanged();
        mDialog.show();
      }

      @Override public void onVolleyError(VolleyError arg0) {
      }
    });
  }
}
